/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/01/12 by CL $
*/
$(function(){
	
	//切换VIP会员组
	$("[f='tab_group']").click(function(){
		$_gid = $(this).attr("data-id");
		$("[f='tab_group']").find("a").removeClass("current");
		$(this).find("a").addClass("current");
		$("#groupid").val($_gid);
		$("#feeid").val("");
		_get_vip_data($_gid);
	});
	
	//选择套餐
	$(document).on("click", "[f='sel_vip_feetype']", function(){
		$feeid = $(this).attr("data-id");
		$grid = $(this).attr("data-groupid");
		$price = parseFloat($(this).attr("data-price"));
		$user_money = parseFloat($("#user_money").val());
		$("[f='sel_vip_feetype']").removeClass("current");
		$(this).addClass("current");
		$("#groupid").val($grid);
		$("#feeid").val($feeid);
		$("#price").val($price);

		if ($user_money < $price) {
			$(".balance").hide();
		}
		$("#pay_total").html($price);
		$(".oe_type_selectpay").show();
	});

	//选择支付方式
	$(document).on("click", "[f='sel_payment']", function(){
		$("[f='sel_payment']").removeClass("current");
		$(this).addClass("current");

		$("#sdkid").val($(this).attr("data-sdkid"));
		$("#sdkname").val($(this).attr("data-sdkname"));
	});
	
	
	//确认升级
	$(document).on("click", "[f='but_vip_confirm_pay']", function(){

		$_sdkid = $("#sdkid").val();
		$_sdkname = $("#sdkname").val();
		$_groupid = $("#groupid").val();
		$_feeid = $("#feeid").val();

		if ($_groupid.length == 0) {
			ToastShow("请选择会员等级");
			return false;
		}
		if ($_feeid.length == 0) {
			ToastShow("请选择购买套餐");
			return false;
		}
		if ($_sdkid.length == 0) {
			ToastShow("请选择支付方式");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _OE_FILE + "&c=vip",
			cache: false,
			data: {a:"confirmpay", groupid:$_groupid, feeid:$_feeid, sdkid:$_sdkid, sdkname:$_sdkname, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					if ($_sdkid == "999") { //余额支付
						ToastShow("开通成功");
						setTimeout(function(){
							window.location.reload();
						}, 300);
					}
					else {
						window.location.href = _ROOT_PATH+"index.php?c=pay&a=submit&paynum="+$result;
					}
				}
				else {
					$_error = $result;
					if ($_error.length == 0) {
						$_error = "开通失败，请检查";
					}
					ToastShow($_error);
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});

});

//获取VIP信息
function _get_vip_data($id) {
	$obj_data = $("#vip_json_data");
	$.ajax({
		type: "POST",
		url: _OE_FILE + "&c=vip",
		cache: false,
		data: {a:"getdata", id:$id, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
			if ($response == "1") {
				if ($obj_data.html().length > 0) {
					$obj_data.fadeOut(JS_FADEOUT_TIME, function(){
						$obj_data.html($result);
						$obj_data.fadeIn(JS_FADEIN_TIME);
					});
				}
				else {
					$obj_data.hide();
					$obj_data.html($result);
					$obj_data.fadeIn(JS_FADEIN_TIME);
				}
			}
			else {
				if ($result.length > 0) {
					ToastShow($result);
				}
				else {
					ToastShow("获取数据错误");
				}
			}
		},
		error: function() {
			ToastShow("系统繁忙，请稍后再试");
		}
	});
}
